<%@page import="com.lynn.salary.service.ISystemFileService"%>
<%@ page contentType="charset=utf-8" pageEncoding="utf-8"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="com.lynn.salary.entity.SystemArgument"%>
<%@page import="com.lynn.salary.service.ISysArgumentService"%>
<%@page import="com.lynn.salary.util.ObjectFactory"%>
<%@page import="com.lynn.salary.entity.SystemFile"%>
<%
	String flag = request.getParameter("flag");
	String fid = request.getParameter("fid");
	String savePath = request.getParameter("savePath");
	String serverPath = application.getRealPath("/");//服务器路径
	System.out.println("$$$$$$$$$$  fid="+fid);
	if(flag == null || flag.trim().equals("") || flag.equals("view")) {
		ISysArgumentService sysArgumentService = (ISysArgumentService)ObjectFactory.getBean("sysArgumentService");
		String rootStorePath = sysArgumentService.getValue("rootStorePath");
		ISystemFileService systemFileService = (ISystemFileService)ObjectFactory.getBean("systemFileService");
		SystemFile sf = systemFileService.getByPk(fid, SystemFile.class);
		
		if(sf==null) {
			System.out.println("fid不存在:"+fid);
			writeFile(getDefaultFile(serverPath),response);
			out.clear();
			out = pageContext.pushBody();
			return;
		}
		
		savePath = sf.getSavePath();
		if(!rootStorePath.endsWith("/") && !rootStorePath.endsWith("\\")){
			rootStorePath=rootStorePath+"/";
		}
		savePath = rootStorePath + savePath;
		
		File f = new File(savePath);
		
		if(!f.exists()) {
			//如果文件不存在
			f = getDefaultFile(serverPath);
			System.out.println("文件不存在2:"+fid);
		}
		
		writeFile(f,response);
		out.clear();
		out = pageContext.pushBody();
	}
//##########################################################
%>

<%! 
	public void writeFile(File f,HttpServletResponse response) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		FileInputStream fis = null;
		try {
			String fileName = f.getName();
			fis = new FileInputStream(f);
			bis = new BufferedInputStream(fis);
	
			BufferedImage image = ImageIO.read(bis);
			String type= f.getName().substring(f.getName().indexOf(".")+1);
			ImageIO.write(image, type, response.getOutputStream());
			fis.close();
		}catch(Exception e) {
			System.out.println("################download2.jsp异常#################");
			e.printStackTrace();
		}
	}

	//获取默认图片文件
	public File getDefaultFile(String serverPath) {
		return new File(serverPath+"\\image_notFound.jpg");
	}
%>